﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Library;

namespace QLKH.PopUp.KhuyenMai
{
    public partial class popSuaKM : Form
    {
        private Int16 idkm = -1;
        public popSuaKM(Int16 id)
        {
            InitializeComponent();
            idkm = id;
        }

        public delegate void SuaKm(int outid);
        // khai báo 1 kiểu hàm delegate
        public SuaKm MyFunction;

        private void btsave_Click(object sender, EventArgs e)
        {
            if (MyFunction != null)
            {// tại đây gọi nó
                string tenkm = txtTenKM.Text.Trim();
                Int16 m3 = Int16.Parse((txtm3.Text.Trim() == "") ? "0" : txtm3.Text.Trim());
                string kieukm = txtKieuKM.Text.Trim();
                Int16 hienthi = Int16.Parse((checkHienthi.Checked) ? "1" : "0");
                string tenkhongdau = Querys.LocDau(txtTenKM.Text.Trim()).ToLower();
                clKM sttp = new clKM();
                int outid = sttp.update(idkm, tenkm, m3,kieukm, hienthi, tenkhongdau);
                MyFunction(outid);
            }
            this.Close();
        }

        private void popSuaKM_Load(object sender, EventArgs e)
        {
            clKM Km = new clKM();
            DataTable dtct = Km.chitiet(idkm);
            if (dtct != null && dtct.Rows.Count > 0)
            {
                txtTenKM.Text = dtct.Rows[0]["ten"].ToString();
                txtm3.Text = dtct.Rows[0]["m3"].ToString();
                txtKieuKM.Text = dtct.Rows[0]["kieu"].ToString();
                checkHienthi.Checked = bool.Parse(dtct.Rows[0]["hienthi"].ToString());
            }
        }

        private void btquit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
